import { ref } from 'vue'
import type { Ref } from 'vue'
import { StoreInterface, ResultInterface, ErrorInterface } from './types/index'

import { getData, postData, putData, deleteData } from '@/api/request'

import axios from 'axios'

export class Store implements StoreInterface {
  // 模型的数据是独立的不应该应该受到其他干扰 专注数据
  view: Ref<{
    data: any[]
    page: number
    pageSize: number
    total: number
    status: number
    loading: boolean
  }>
  // 分页器的独立查询条件
  filterParams: Ref<{
    page: number
    pageSize: number
    limit: number
  }>

  private controller: AbortController | null = null

  // 受保护的属性
  protected url: string = ''

  constructor(data: object[] = []) {
    this.view = ref({
      data,
      page: 1,
      pageSize: 10,
      total: 0,
      status: 0,
      loading: false
    })

    this.filterParams = ref({
      page: 1,
      pageSize: 10,
      limit: 10
    })
  }

  async searchData(baseParams: { page: number; pageSize: number; limit: number }): Promise<void> {
    let params = {
      ...baseParams,
      // 查询条件 丢失响应式是可以接受的
      ...this.filterParams.value
    }

    // 请求取消
    this.abortRequest()
    this.controller = new AbortController()

    try {
      //  1 开始加载
      this.status = 1
      const result = await getData(this.url, {
        data: params,
        signal: this.controller.signal
      })
      // 根据结果传递组合参数
      if (this.getSuccess(result)) {
        // 成功状态 2
        this.status = 2
        this.data = this.getData(result)
        this.page = this.getPage(result)
        this.pageSize = this.getPageSize(result)
        this.total = this.getTotal(result)
      }
    } catch (error: unknown) {
      if (axios.isAxiosError(error)) {
        // 确定是 Axios 错误
        // 说明是取消请求而不是请求失败 这时的this.status不应该是3 而应该是1
        if (error.code === 'ECONNABORTED') {
          this.status = 1
        }
      } else if (error instanceof Error) {
        // 暂时不处理
        // 其他 Error 类型错误
        // 失败状态 3
        this.status = 3
        console.log('错误:', error.message)
      } else {
        // 失败状态 3
        this.status = 3
        // 处理非 Error 类型的异常
        console.log('发生错误:', error)
      }
    }
  }
  addData() {}
  deleteData() {}
  updateData() {}
  abortRequest(): void {
    this.controller?.abort()
  }

  getSuccess(res: ResultInterface): boolean {
    return res?.code === 200
  }
  getData(res: ResultInterface): object[] {
    return res?.data?.data || []
  }
  getPage(res: ResultInterface) {
    return res?.data?.page || 1
  }
  getPageSize(res: ResultInterface) {
    return res?.data?.pageSize || 10
  }
  getTotal(res: ResultInterface) {
    return res?.data?.total || 0
  }

  // 访问器属性
  get data(): object[] {
    return this.view.value.data
  }
  set data(value: object[]) {
    this.view.value.data = value
  }
  get page(): number {
    return this.view.value.page
  }
  set page(value: number) {
    this.view.value.page = value
  }
  get pageSize(): number {
    return this.view.value.pageSize
  }
  set pageSize(value: number) {
    this.view.value.pageSize = value
  }
  get total(): number {
    return this.view.value.total
  }
  set total(value: number) {
    this.view.value.total = value
  }
  get status(): number {
    return this.view.value.status
  }
  set status(value: number) {
    this.view.value.status = value
  }
}
